package com.xsmart.order.dao;

import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author  pansheng
 * on 2017/12/14.
 */
@Component
public interface OrderFillsDao {

    /**   查询订单信息
     * @param page    页数
     * @param orderSn  订单号
     * @param orderStatus   订单状态
     * @param orderId   订单ID
     * @return  返回记录数
     */
    @Select("<script>" +
            "select t.order_id,ss.shop_name,u.nick_name,ss.id,t.user_id,t.shipping_id,t.pay_id," +
            "t.order_sn,(CASE WHEN order_status = '0' THEN '未确认' WHEN order_status = '1' THEN '确认' WHEN order_status = '2' THEN '已取消' WHEN order_status = '3' THEN '无效' WHEN order_status = '4' THEN '退货' END)order_status," +
            "(CASE WHEN shipping_status = '0' THEN '未发货' WHEN shipping_status = '1' THEN '已发货' WHEN shipping_status = '2' THEN '已收货' WHEN shipping_status = '3' THEN '退货' WHEN shipping_status = '4' THEN '备货中' END)shipping_status," +
            "(CASE WHEN pay_status = '0' THEN '未付款' WHEN pay_status = '1' THEN '付款中' WHEN pay_status = '2' THEN '已付款' END)pay_status," +
            "t.postscript,t.goods_amount,t.shipping_fee," +
            "iua.address,iua.mobile,iua.best_time,iua.tel,iua.sign_building,iua.zipcode,iua.email,iua.consignee," +
            "t.money_paid,t.integral,t.integral_money,t.bonus,t.order_amount,p.pay_name,s.shipping_name,u.user_name, " +
            "DATE_FORMAT(t.add_time,'%Y-%m-%d %T')add_time, " +
            "DATE_FORMAT(t.confirm_time,'%Y-%m-%d %T')confirm_time," +
            "DATE_FORMAT(t.pay_time,'%Y-%m-%d %T')pay_time," +
            "DATE_FORMAT(t.shipping_time,'%Y-%m-%d %T')shipping_time," +
            "(select min(oga_service) " +
            "from order_goods_appraise oga,order_info oi where   oga.order_id = t.order_id and oga.order_id in  " +
            "(select order_id from order_info) " +
            "GROUP BY oga.order_id )  oga_service, " +
            "(select min(oga_shipping) " +
            "from order_goods_appraise oga,order_info oi where   oga.order_id = t.order_id and oga.order_id in  " +
            "(select order_id from order_info) " +
            "GROUP BY oga.order_id )  oga_shipping, " +
            "(select min(oga_goods) " +
            "from order_goods_appraise oga,order_info oi where   oga.order_id = t.order_id and oga.order_id in  " +
            "(select order_id from order_info) " +
            "GROUP BY oga.order_id )  oga_goods " +
            "from order_info t " +
            "LEFT JOIN INFS_USER u on t.user_id =u.user_id " +
            "LEFT JOIN shipping s on t.shipping_id = s.shipping_id " +
            "LEFT JOIN payment p on t.pay_id =p.pay_id " +
            "LEFT JOIN order_shipping_info osi on t.order_id = osi.order_id " +
            "LEFT JOIN infu_user_address iua on t.user_id = iua.user_id " +
            "left outer join infu_distributor_member dm on dm.user_id=t.user_id " +
            "left outer join seller_shopinfo ss on ss.ru_id=dm.d_user_id " +
            "<where> " +
            "1=1 " +
            "<if test=\"orderSn!=null and orderSn!=''\">and t.order_sn=#{orderSn} </if> " +
            "<if test=\"orderStatus!=null and orderStatus!=''\"> and t.order_status=#{orderStatus} </if> " +
            "<if test=\"payStatus!=null and payStatus!=''\"> and t.pay_status=#{payStatus} </if> " +
            "<if test=\"shippingStatus!=null and shippingStatus!=''\"> and t.shipping_status=#{shippingStatus} </if> " +
            "<if test=\"payStatus==0 or shippingStatus==0\"> and t.order_status not in(2,3,4,5)</if> " +
            "<if test=\"shippingStatus==0\"> and t.pay_status in(2)</if> " +
            "<if test=\"shopName!=null and shopName!='' and (selectType=='false' or  selectType==null) and (platform==null or platform=='false')\"> and ss.shop_name like concat('%',#{shopName},'%') </if> " +
            "<if test=\"selectType=='true' and shopName!=null and shopName!='' \"> and ss.shop_name =#{shopName}  </if> " +
            "<if test=\"platform=='true' \"> and dm.d_user_id=-1   </if> " +
            "<if test=\"orderId!=null\">and t.order_id=#{orderId}</if>" +
            "</where> GROUP BY t.order_id " +
            "order by t.order_id" +
            "</script>")
    List<Map<String,Object>> queryOrder(Pagination page, @Param("orderSn") String orderSn, @Param("orderStatus") String orderStatus,@Param("payStatus")String payStatus,@Param("shippingStatus")String shippingStatus,@Param("orderId")String orderId,@Param("shopName") String shopName,@Param("selectType") String selectType,@Param("platform")String platform);

    /**
     * 创建订单
     *
     * @param orderMap 订单信息
     * @return 返回影响记录数
     */
    @Insert("INSERT INTO order_info(user_id\t,\n" +
            "shipping_id,\n" +
            "pay_id,\n" +
            "order_sn,\n" +
            "postscript,\n" +
            "goods_amount,\n" +
            "shipping_fee,\n" +
            "order_amount," +
            "seller_id)" +
            "VALUES" +
            "(#{user_id},\n" +
            "#{shipping_id},\n" +
            "#{pay_id},\n" +
            "#{order_sn},\n" +
            "#{postscript},\n" +
            "#{goods_amount},\n" +
            "#{shipping_fee},\n" +
            "#{order_amount}," +
            "(SELECT t1.id from seller_shopinfo t1 " +
            "LEFT JOIN infu_distributor_member t2 on t2.d_user_id = t1.ru_id " +
            "where t2.user_id = #{user_id}))")
    @Options(useGeneratedKeys = true, keyProperty = "order_id", keyColumn = "order_id")
    int addOrder(Map<String, Object> orderMap);

    /**
     * 添加订单商品
     * @param goodsMap 订单商品
     * @return 影响记录数
     */
    @Insert("insert into order_goods(order_id,\n" +
            "goods_id,\n" +
            "goods_sn,\n" +
            "goods_name,\n" +
            "goods_number,\n" +
            "cost_price,\n" +
            "market_price,\n" +
            "goods_price,\n" +
            "goods_attr,\n" +
            "goods_attr_id,\n" +
            "is_gift,\n" +
            "is_real,\n" +
            "parent_goods_id)" +
            "values(#{order_id},\n" +
            "#{goods_id},\n" +
            "#{goods_sn},\n" +
            "#{goods_name},\n" +
            "#{goods_number},\n" +
            "#{cost_price},\n" +
            "#{market_price},\n" +
            "#{goods_price},\n" +
            "#{goods_attr},\n" +
            "#{goods_attr_id},\n" +
            "#{is_gift},\n" +
            "#{is_real},\n" +
            "#{parent_goods_id})")
    int addOrderGoods(Map<String,Object> goodsMap);

    /**
     * 添加配送地址
     * @param orderShippingMap 配送地址
     * @return 影响记录数
     */
    @Insert("INSERT INTO order_shipping_info(order_id,\n" +
            "consignee,\n" +
            "full_address,\n" +
            "email,\n" +
            "country,\n" +
            "province,\n" +
            "city,\n" +
            "district,\n" +
            "street,\n" +
            "address,\n" +
            "zipcode,\n" +
            "tel,\n" +
            "mobile,\n" +
            "sign_building,\n" +
            "best_time)" +
            "values(#{order_id},\n" +
            "#{consignee},\n" +
            "#{full_address},\n" +
            "#{email},\n" +
            "#{country},\n" +
            "#{province},\n" +
            "#{city},\n" +
            "#{district},\n" +
            "#{street},\n" +
            "#{address},\n" +
            "#{zipcode},\n" +
            "#{tel},\n" +
            "#{mobile},\n" +
            "#{sign_building},\n" +
            "#{best_time})")
    int addOrderShippingInfo(Map<String,Object> orderShippingMap);


    @Update("UPDATE order_info " +
            "SET " +
            "order_status = #{orderMap.order_status}, " +
            "shipping_status= #{orderMap.shipping_status}, " +
            "where order_id = #{orderMap.order_id} ")
    int modOrder(@Param("orderMap") Map orderMap);


    /**
     * @param page
     * @param orderId
     * @return 订单详情
     */
    @Select("<script>" +
            "SELECT t.order_goods_id,t.order_id,t.goods_id," +
            "t.goods_sn,t.goods_name,t.goods_number,t.market_price," +
            "t.goods_price,t.goods_attr,t.goods_attr_id,t.goods_attr," +
            "t.is_real,t.parent_goods_id,g.goods_img  " +
            "FROM order_goods t " +
            "LEFT JOIN order_info o ON t.order_id = o.order_id " +
            "LEFT JOIN goods g ON t.goods_id = g.goods_id " +
            "WHERE t.order_id = #{orderId}" +

            "</script>")
    List<Map<String, Object>> queryGoods(Pagination page, @Param("orderId") Integer orderId);

    @Select("SELECT min(oga_service),min(oga_shipping),min(oga_goods) " +
            "FROM order_goods_appraise oga,order_info oi " +
            "WHERE oga.order_id = oi.order_id AND oga.order_id IN " +
            "(SELECT order_id FROM order_info) " +
            "GROUP BY oga.order_id ")
    List<Map<String, Object>> queryAppraise();


    /**
     * 查询商品的评价
     * @param orderId
     * @return
     */
    @Select("Select o.oga_text,DATE_FORMAT(t.confirm_time,'%Y-%m-%d %T')confirm_time,DATE_FORMAT(t.pay_time,'%Y-%m-%d %T')pay_time, DATE_FORMAT(t.shipping_time,'%Y-%m-%d %T')shipping_time from order_info t left join order_goods_appraise o on  t.order_id=o.order_id where t.order_id =#{orderId}")
     List<Map<String,Object>> queryOgaText(@Param("orderId")Integer orderId);

    /**
     * 查询操作信息
     * @param orderId
     * @return
     */
    @Select("select o.order_action_id,i.user_name," +
            "(CASE WHEN o.order_status = '0' THEN '未确认' WHEN o.order_status = '1' THEN '确认' WHEN o.order_status = '2' THEN '取消' WHEN o.order_status = '3' THEN ' 无效' WHEN o.order_status = '3' THEN ' 取消' END)order_status," +
            "(CASE WHEN shipping_status = '0' THEN '未发货' WHEN shipping_status = '1' THEN ' 已发货' WHEN shipping_status = '2' THEN ' 已收货' WHEN shipping_status = '3' THEN ' 退货' WHEN shipping_status = '4' THEN ' 备货中' END)shipping_status,"  +
            "(CASE WHEN o.pay_status = '0' THEN '未付款' WHEN o.pay_status = '1' THEN '付款中' WHEN o.pay_status = '2' THEN '已付款'  END)pay_status," +

            "o.action_note,DATE_FORMAT(o.log_time,'%Y-%m-%d %T')log_time from order_action o,INFS_ADMIN_USER i where order_id=#{orderId} and o.user_id=i.user_id")
    List<Map<String,Object>>queryOptionInfo(@Param("orderId")Integer orderId);

    /**
     * 插入操作信息
     * @param optionInfo
     * @return
     */
    @Insert("insert into order_action(user_id,order_id,order_status,shipping_status,pay_status,action_note,log_time)values(" +
            "#{optionInfo.userId}," +
            "#{optionInfo.orderId}," +
            "#{optionInfo.orderStatus}," +
            "#{optionInfo.shippingStatus}," +
            "#{optionInfo.payStatus}," +
            "#{optionInfo.actionNote}," +
            "now())")
    int insertOrderAction(@Param("optionInfo")Map<String,Object>optionInfo);

    /**
     * 更改订单的状态
     * @param orderStatus
     * @param shippingStatus
     * @param orderId
     * @return
     */
    @Update("<script>update order_info " +
            "<set>" +
            "<if test=\"actionType!=null and actionType==1\">order_status=#{orderStatus},confirm_time=now()</if>" +
            "<if test=\"orderStatus!=null and orderStatus==2\">order_status=#{orderStatus}</if>" +
            "<if test=\"shippingStatus!=null and shippingStatus==1\">shipping_status=#{shippingStatus},shipping_time=now()</if> " +
            "<if test=\"shippingStatus!=null and shippingStatus==4\">shipping_status=#{shippingStatus}</if> " +
            "</set>" +
            "where order_id=#{orderId}" +
            " </script>")
    int updateOrderStatus(@Param("orderStatus") String orderStatus,@Param("shippingStatus") String shippingStatus,@Param("orderId") Integer orderId);
}
